package gew.beans;

import gew.data.TreeItem;
import gew.metadata.CustomSql;
import gew.services.DimensionValueService;
import gew.sql.SqlTemplates;
import gew.system.Constants;
import gew.system.IRequestParameterTree;
import gew.system.RequestParameterTree;
import gew.utils.SqlUtils;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean(name = "treeBn")
@RequestScoped
public class TreeBean {

    @EJB
    private DataFacade2 dataFacade2;
    
    @EJB
    DimensionValueService dvs;

    IRequestParameterTree rp;
    private ArrayList<TreeItem> items;
    
    private String selectedIds;
    private String urlTemplate;
    private String filterRequestType;
    private String filterCode;
    
    IRequestParameterTree getRequestParameter() {
        return rp;
    }
    
    void initRequestParameter(){
        rp = new RequestParameterTree((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(), dvs);
    }
    
    private void setSelectedIds(){
        selectedIds = rp.getSelectedIdsCS().replace(SqlUtils.QUOTE, SqlUtils.EMPTY).replace(SqlUtils.HYPHEN, SqlUtils.COMMA);
    }
    
    private void setUrlTemplate(){
        urlTemplate = rp.getUrlTemplate();
    }
    
    private void setFilterRequestType(){
        filterRequestType = rp.getFilterRequestType();
    }
    
    private void setFilterCode(){
        filterCode = rp.getFilterCode();
    }

    @PostConstruct
    public void init() {
        initRequestParameter();        
        setSelectedIds();
        setFilterRequestType();
        setFilterCode();
        setUrlTemplate();
        String[] urls = urlTemplate.split(SqlUtils.AT);
        String urlBegin = urls.length > 0 ? urls[0].replace(SqlUtils.VERTICAL_BAR, SqlUtils.SLASH) : SqlUtils.EMPTY;
        String urlVar = urls.length > 1 ? urls[1].replace(SqlUtils.VERTICAL_BAR, SqlUtils.SLASH) : SqlUtils.EMPTY;
        String urlEnd = urls.length > 2 ? urls[2].replace(SqlUtils.VERTICAL_BAR, SqlUtils.SLASH) : SqlUtils.EMPTY;
        
        String q1 = SqlUtils.EMPTY;
        switch (filterRequestType) {
            case Constants.FILTER_REQUEST_INIT: {
                q1 = CustomSql.getTree2Init(rp.getLocaleUrlCode(), filterCode, selectedIds, urlBegin, urlVar, urlEnd);
            }
            break;
            case Constants.FILTER_REQUEST_CHILDREN: {
                q1 = CustomSql.getTree2Children(rp.getLocaleUrlCode(), filterCode, selectedIds, urlBegin, urlVar, urlEnd);
            }
            break;
            case Constants.FILTER_REQUEST_SEARCH: {
                String q2 = CustomSql.getTree2SearchIds(filterCode, rp.getFilterText());
                List<String> searchResults = dataFacade2.executeQueryToListOfString(q2);
                searchResults.add(selectedIds);
                String filteredIds = String.join(SqlUtils.COMMA, searchResults);
                String additionalCondition = SqlUtils.EMPTY;
                if (filterCode.equals(((Integer)Constants.HIER_GROUP_COMMODITY).toString())) {
                    additionalCondition = SqlTemplates.getSql(SqlTemplates.TREE_SEARCH_COMMODITY);
                }
                q1 = CustomSql.getTree2Search(rp.getLocaleUrlCode(), filterCode, selectedIds, urlBegin, urlVar, urlEnd, filteredIds, additionalCondition);
            }
            break;
        }
        if (SqlUtils.EMPTY.equals(q1)) {
            items = new ArrayList<>();
        } else {
            items = dataFacade2.getTreeItems(q1);
        }
    }

    public ArrayList<TreeItem> getItems() {
        return items;
    }

}
